import { Injectable } from "@nestjs/common"
import { CopyrightEntity } from "./copyright.entity"
import { InjectRepository } from "@nestjs/typeorm"
import { Repository } from "typeorm"

@Injectable()
export class CopyrightService {
  constructor(
    @InjectRepository(CopyrightEntity)
    private readonly copyrightEntity: Repository<CopyrightEntity>
  ) {}

  /**
   * Obtain copyright configuration
   * @param res
   */
  async getConfig(res: any): Promise<void> {
    try {
      const result = await this.copyrightEntity.findOne({ where: { id: 1 } })
      res.json(result)
    } catch (err) {
      console.log(err)
    }
  }

  /**
   * Save the copyright configuration
   * @param body
   * @param res
   */
  async saveConfig(body: any, res: any): Promise<void> {
    try {
      await this.copyrightEntity.save(body)
      res.json({ status: "success", message: "设置保存成功！" })
    } catch (err) {
      console.log(err)
      res.json({ status: "error", message: "服务器内部错误，请稍后再试！" })
    }
  }
}
